#include "http-session-on-ssl.h"

HttpSessionOnSSL::HttpSessionOnSSL(HttpServer &server, boost::asio::ssl::stream<tcp::socket> &&socket)
	: HttpSession(server)
	, socket_(std::move(socket))
{

	asyncHandshake();

	// HTTPS 会话的第一步是建立 SSL 连接，所以不要在构造函数里通知基类开始工作
}

HttpSessionOnSSL::~HttpSessionOnSSL()
{
}

void HttpSessionOnSSL::asyncReadHttpRequest(UnderRequestBuffer &requestBuffer, UnderRequest &request, std::function<void(std::error_code ec, size_t readn)> &&cb)
{
	boost::beast::http::async_read(socket_, requestBuffer, request, std::move(cb));
}

void HttpSessionOnSSL::asyncWriteHttpResponse(const UnderResponse &response, std::function<void(std::error_code ec, size_t readn)> &&cb)
{
	boost::beast::http::async_write(socket_, response, std::move(cb));
}

void HttpSessionOnSSL::asyncHandshake()
{
	socket_.async_handshake(boost::asio::ssl::stream_base::server, [this](const boost::system::error_code &error) {
		if (error) {
			stop();
		} else {
			startHandleMessage();
		}
	});
}
